﻿<script type="text/javascript">
	var shippings = {$shippings};
	var addresses = {$addresses};
	var goods_amount = {$goods_info.amount};
	var goods_quantity = {$goods_info.quantity};
	$(function(){
		
		/* 订单总额初始化 */
		fill_order_amount();
					
		/*  收货地址初始化 */
		fill_address_info($('.J_AddressEach').find('input[name="addr_id"]').val());
		
		/* 初始化验证收货地址是否支持货到付款 */
		check_cod_enable($('.J_AddressEach').find('input[name="addr_id"]').val());
					 
		$('.J_LogistFeesSelect').on('change', function(){
			fill_order_amount();
		});
		
		$('.J_AddressEach').click(function(){
			$(this).parent().children().removeClass('selected_address');
			$(this).addClass('selected_address');
			$(this).parent().children().find('input[name="addr_id"]').prop('checked' ,false);
			$(this).find('input[name="addr_id"]').prop('checked', true);
			
			var addr_id = $(this).find('input[name="addr_id"]').val();
			
			/* 选中地址后，自动收缩地址列表 */
			select_ship_address();
			
			/* 加载该收货地址对应的运费 */
			fill_logist_fee_by_address(addr_id);
						
			/* 赋值收货地址 */
			//fill_address_info(addr_id);
			
			/* 验证收货地址是否支持货到付款 */
			check_cod_enable(addr_id);
						
			/* 更新订单总额 */
			fill_order_amount();
						
		});
		$('.J_MoreAddress').click(function(){
			select_ship_address();
		});
					
	});
	
	function check_cod_enable(addr_id)
	{
		var  store_id = {$smarty.get.store_id};
		var url = REAL_SITE_URL+'/index.php?app=order&act=ajax_cod_enable'; 

		$.getJSON(url,{'addr_id':addr_id,'store_id':store_id},function(data){
			if(data.done)
			{
				$('#enable_cod').removeAttr('disabled');
			}
			else
			{
				$('#enable_cod').attr('disabled','disabled').removeAttr('checked');
			}
		})
	}
	
	function select_ship_address()
	{
		$('.J_AddressEach').parent().find('dl').toggleClass('hidden');
		$('.J_AddressEach').parent().find('.selected_address').removeClass('hidden');
		if($('.J_AddressEach').parent().find('.hidden').length > 0) {
			$('.J_MoreAddress').show();
			//$('.J_MoreAddress').html('{$lang.expand}');
		} else {
			$('.J_MoreAddress').hide();
			//$('.J_MoreAddress').html('{$lang.fold}');
		}
	}
				
	/* 赋值收货地址 */
	function fill_address_info(addr_id)
	{
		address = addresses[addr_id];
		phone = address['phone_mob'];
		if(phone=='') phone = address['phone_tel'];
		$('.J_AddressDetail').html(address['region_name'] + ' ' + address['address']);
		$('.J_Consignee').html(address['consignee'] + ' ' + phone);
	}
				
	/* 加载该收货地址对应的运费 */
	function fill_logist_fee_by_address(addr_id)
	{
		$('.J_LogistFeesSelect').children().remove();
		shipping_data = shippings[addr_id];
			
		$.each(shipping_data,function(k,v) {
			html = '<option value="'+k+'" price="'+v.logist_fees+'">'+v.name+'：'+number_format(v.logist_fees,2)+'</option>';
			$('.J_LogistFeesSelect').append(html);
		});
	}
				
	/* 设置总费用 */
	function fill_order_amount()
	{
		logist_fee = coupon_value = integral_value = 0;
	
		logist_fee = parseFloat($('.J_LogistFeesSelect').find('option:selected').attr('price'));
					
		<!--{if $goods_info.integral_enabled && !$yz_integral.goods_disable_use_integral}-->
		if($('.J_UseIntegralCheckbox').prop('checked')==true && $('.J_IntegralAmount').val()>0){
			integral_value = number_format(parseFloat($('.J_IntegralAmount').val()) * {$exchange_rate}, 2);
			$('.J_IntegralPrice').html(integral_value);
		} else $('.J_IntegralPrice').html(0);
		<!--{/if}-->
					
		<!--{if $goods_info.allow_coupon}-->
		if($('.J_UseCouponSelect').val() != ''){
			coupon_value = parseFloat($('.J_UseCouponSelect').find('option:selected').attr('price'));
		}
		<!--{/if}-->
		
		$('.J_LogistFees').html(number_format(logist_fee,2));
		$('.J_OrderAmount').html(number_format((goods_amount+logist_fee-coupon_value-integral_value).toFixed(2), 2));
	}
</script>

<div id="select-address">
	<div class="title mb10">
		<!--<b class="fs14">{$lang.consignee_address}</b> <a href="{url app=my_address}" target="_blank">[{$lang.manage_address}]</a>-->
	</div>
	<!--{if $my_address}-->
	<div class="oldaddress clearfix">
		<!--{foreach from=$my_address item=address name=fe_address}-->
		<dl class="clearfix {if $smarty.foreach.fe_address.first} selected_address{/if} {if !$smarty.foreach.fe_address.first} hidden {/if} J_AddressEach" >
			<dt>
				<input type="checkbox" name="addr_id" value="{$address.addr_id}" {if $smarty.foreach.fe_address.first} checked="checked" {/if}/>
				<b>{$lang.consignee}{$lang.colon}{$address.consignee|escape} <!--{if $address.phone_mob}-->{$address.phone_mob}<!--{else}-->{$address.phone_tel}<!--{/if}--> </b>
			</dt>
			<dd class="addr-bd"> {$address.region_name} {$address.address|escape}</dd>
		</dl>
		<!--{/foreach}-->
	</div>
	<!--{/if}-->
	<div class="use-new-address clearfix">
		<a href="{url app=my_address&act=add}" class="btn-new-addr" >{$lang.use_new_address}</a>
		<a href="javascript:;" class="btn-more-addr J_MoreAddress" >&nbsp;</a>
	</div>
</div>
